package com.example.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.draw.ImageResDrawFormat;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.table.TableData;
import com.bin.david.form.listener.OnColumnItemClickListener;
import com.example.fragment.tool.DataBaseTool;
import com.example.fragment.tool.GremSetDialog;
import com.example.fragment.tool.User;

import java.util.ArrayList;
import java.util.List;

public class GrameSettingFragment extends Fragment {

    private ImageView imClose;  //右上角关闭按钮
    private ImageView mIvAddGrem;   //右下角添加菌种按钮

    private static final String TABLE_NAME1 = "Plan1";
    private SmartTable<User> table;
    private ArrayList<Integer> iddelete ;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_grame_setting_fragment, container,false);
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        table = getActivity().findViewById(R.id.table);
        imClose = getActivity().findViewById(R.id.iv_close);
        imClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            MainActivity myActivity = (MainActivity) getActivity();
                myActivity.initSettingFragment();
            }
        });

        mIvAddGrem = getActivity().findViewById(R.id.iv_addgrem);
        mIvAddGrem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final GremSetDialog gremSetDialog =new GremSetDialog(getActivity());
                gremSetDialog.setmBtnPositive(new GremSetDialog.onPositiveListener() {
                    @Override
                    public void onpisitive(GremSetDialog dialog) {
                        String grem =gremSetDialog.getmEtGrem();
                        String time =gremSetDialog.getmEtTime();
                        String temprature =gremSetDialog.getmEtTemperature();

                        //判断是否为空
                        if(gremSetDialog.getmEtGrem().equals("")||gremSetDialog.getmEtTime().equals("")||gremSetDialog.getmEtTemperature().equals("")){
                            Toast.makeText(getActivity(),"信息不能为空", Toast.LENGTH_LONG).show();
                        }else {
                            DataBaseTool.add(getActivity(),1,"2020-6-20", grem,temprature,time,TABLE_NAME1);
                            refresh();
                        }
                    }
                }).show();

            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkColumn.setOnColumnItemClickListener(new OnColumnItemClickListener<Boolean>() {
            @Override
            public void onClick(Column<Boolean> column, String value, Boolean aBoolean, int position) {
                iddelete =DataBaseTool.getid(getContext(),1,TABLE_NAME1);
                DataBaseTool.delete(getContext(),1,TABLE_NAME1,iddelete.get(position));
                refresh();
                iddelete.clear();
            }
        });
        change.setOnColumnItemClickListener(new OnColumnItemClickListener<Boolean>() {
            @Override
            public void onClick(Column<Boolean> column, String value, Boolean aBoolean, final int position) {
                iddelete =DataBaseTool.getid(getContext(),1,TABLE_NAME1);
                final GremSetDialog gremSetDialog =new GremSetDialog(getContext());
                gremSetDialog.setmBtnPositive(new GremSetDialog.onPositiveListener() {
                    @Override
                    public void onpisitive(GremSetDialog dialog) {
                        if(gremSetDialog.getmEtGrem().equals("")||gremSetDialog.getmEtTime().equals("")||gremSetDialog.getmEtTemperature().equals("")){
                            Toast.makeText(getContext(),"信息不能为空", Toast.LENGTH_LONG).show();
                        }else {
                            DataBaseTool.changdata(getContext(),1,TABLE_NAME1,iddelete.get(position),"2020-6-20", gremSetDialog.getmEtGrem(),gremSetDialog.getmEtTemperature(),gremSetDialog.getmEtTime(),null,null,null);
                            refresh();}
                    }
                }).setmEtGrem(germ.getDatas().get(position))
                        .setmEtTime(timer.getDatas().get(position))
                        .setmEtTemperature(timelong.getDatas().get(position))
                        .setmTvTitle("参数修改").show();


            }
        });
        final List<User> list=dodata();
        //表格数据 datas 是需要填充的数据
        final TableData<User> tableData = new TableData<>("测试", list, data,germ, timer,timelong, change, checkColumn);
//设置数据
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                table.setTableData(tableData);
                table.getConfig().setShowXSequence(false);//取消最右侧的123
                table.getConfig().setShowYSequence(false);//取消abc
                table.getConfig().setShowTableTitle(false);//取消大标题
                table.getConfig().setContentStyle(new FontStyle(18, Color.parseColor("#00e9ae")));//内容的字体设置
                table.getConfig().setColumnTitleStyle(new FontStyle(20,Color.parseColor("#00e9ae")));//标题的字体设置

            }
        },1500);

    }

    final Column<String> data = new Column<>("添加/修改日期", "data");
    final Column<String> timer = new Column<>("温度", "timer");
    final Column<String> timelong = new Column<>("时长", "timelong");
    final Column<String> germ = new Column<>("菌种", "germ");
    Column<Boolean> checkColumn = new Column<>("删除", "isCheck",new ImageResDrawFormat<Boolean>(70,36) {
        @Override
        protected Context getContext() {
            return getActivity();
        }
        @Override
        protected int getResourceID(Boolean isCheck, String value, int position) {
            if(isCheck){
                return R.mipmap.deleteui1;
            }
            return 0;
        }
    });
    Column<Boolean> change = new Column<>("修改", "change", new ImageResDrawFormat<Boolean>(64,32) {
        @Override
        protected Context getContext() {
            return getActivity();
        }

        @Override
        protected int getResourceID(Boolean aBoolean, String value, int position) {
            if(aBoolean){
                return R.mipmap.change1;
            }
            return 0;
        }
    });

    //刷新表格数据
    private void refresh(){
        List<User> listdelete=dodata();
        TableData<User> tableData1 = new TableData<User>("测试", listdelete, data,germ, timer,timelong, change, checkColumn);
        table.setTableData(tableData1);
    }

    //将数据库中的数据，填充到数组中
    private List<User> dodata(){
        List<User> list = new ArrayList<>();
        try {
            ArrayList<ArrayList<String>> show = DataBaseTool.getlist4(getActivity(),1,TABLE_NAME1);
//设置单元格内容

            if(show!=null) {
                for (int i = 0; i < show.size(); i++) {
                    ArrayList<String> a1 = show.get(i);
                    String a = a1.get(0);
                    String b = a1.get(1);
                    String c = a1.get(2)+"℃";
                    String d = a1.get(3)+"h";
                    list.add(new User(a, b, c, d, true, true));
                }}
        }catch (Exception e){}

        return list;
    }
}
